package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.Lehrer;

public class Dao_Lehrer
{
	
private Dao_Lernbuero lernbueroDAO=new Dao_Lernbuero();
	
	public Lehrer getLehrer(String lname)
	{
		
		
		Connection conn=ConnectionProvider.getConnection();
		PreparedStatement statement=null;
		ResultSet rs=null;
		Lehrer l=new Lehrer();
		String sql="select * from lehrer where lname=?";
        try {
			statement = conn.prepareStatement(sql);
			statement.setString(1, lname);
			rs=statement.executeQuery();
			
			if (rs.next())
			{
				l=new Lehrer();
				l.setLeherID(1);
				l.setLnachname(rs.getString("lname"));
				l.setLvorname(rs.getString("lvorname"));
				l.setPasswort(rs.getString("passwort"));
				l.setSuperUser(rs.getInt("superuser")==0?false:true);
				l.setLernbuero(lernbueroDAO.getLernbuero(rs.getInt("lbid")));
			}
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		finally {
			try {
				rs.close();
				statement.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return l;
	}
	
	public void update(Lehrer l)
	{
		Connection conn=ConnectionProvider.getConnection();
		String sql="update Lehrer set lehrerID=?, lvorname=?, passwort=?, superuser=?, lbid=? where lname=?";
        try {
        	
			PreparedStatement statement = conn.prepareStatement(sql);
			statement.setInt(1, l.getLeherID());
			statement.setString(2, l.getLnachname());
			statement.setString(3, l.getLvorname());
			statement.setString(4, l.getPasswort());
			statement.setInt(5, l.getSuperUser()?1:0);
			statement.setInt(6, l.getLernbuero().getLbID());
			

			statement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void anlegen(Lehrer l)
	{
		Connection conn=ConnectionProvider.getConnection();
		String sql="insert into Lehrer VALUES (?, ?, ?, ?, ?, ?)";
        try {
			PreparedStatement statement = conn.prepareStatement(sql);
			statement.setInt(1, l.getLeherID());
			statement.setString(2, l.getLvorname());
			statement.setString(3, l.getLnachname());
			statement.setString(4, l.getPasswort());
			statement.setInt(5, l.getSuperUser()?1:0);
			statement.setInt(6, l.getLernbuero().getLbID());

			statement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void delete(int id)
	{
		Connection conn=ConnectionProvider.getConnection();
		String sql="delete from Lehrer where lehrerid= ?";
        try {
			PreparedStatement statement = conn.prepareStatement(sql);
			statement.setInt(1, id);
			statement.executeUpdate();
        } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
        finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public Lehrer getLeherID(int int1) 
	{
		// TODO Auto-generated method stub
		return new Lehrer();
	}

}
